package ch03_oothinking.serializable;

import java.io.*;

/**
 *
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class StrSerializable {
    public static void main(String[] args) {
        writeObject();
        readObject();
    }

    public static void writeObject() {
        String filePath = "data.ser";
        String message = "Java Serialization is Cool";
        try (
                FileOutputStream fos = new FileOutputStream(filePath);
                ObjectOutputStream outputStream = new ObjectOutputStream(fos);
        ) {
            outputStream.writeObject(message);
        } catch (IOException ex) {
            System.err.println("IO error: " + ex);
        }
    }

    public static void readObject() {
        String filePath = "data.ser";
        try (
                FileInputStream fis = new FileInputStream(filePath);
                ObjectInputStream inputStream = new ObjectInputStream(fis);
        ) {
            String message = (String) inputStream.readObject();
            System.out.println("Message: " + message);
        } catch (ClassNotFoundException ex) {
            System.err.println("Class not found: " + ex);
        } catch (IOException ex) {
            System.err.println("IO error: " + ex);
        }
    }
}
